package com.just521.xinshoutong;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.just521.xinshoutong.common.CommonDefine;
import com.just521.xinshoutong.dialog.CustomDlg;
import com.mobclick.android.MobclickAgent;

public class ActivityBase extends Activity 
{
	 boolean mbIsQuit;
	 byte[] mlock=new byte[0];
	 private BroadcastReceiver  br;
	 protected  boolean		reciver = false;
	 QuitListener 	mQuitListener;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 requestWindowFeature(Window.FEATURE_NO_TITLE); 
//		 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		 
		 
	        IntentFilter intentFilter = new IntentFilter();
	        intentFilter.addAction("mylover.finish.action.broadcast");
	        br = new MyBroadcastReciver();
	        this.registerReceiver(br, intentFilter);
	        
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) { 
			if(mQuitListener!=null){
				mQuitListener.execute(this);
				return true;
			}
        } 
        return super.onKeyDown(keyCode, event); 
    }
	/*
	 *取得字符串 
	 * 
	 */
	public String getstring( int id ){
		return CommonDefine.getResources(this).getString(id);
	}
	
	
	public void show_toast(int id){
		Toast.makeText(this, getstring(id),
				2000).show();
	}
	
	public void show_toast(String str){
		Toast.makeText(this, str,
			     2000).show();
	}
	
	
	/*
	 *取得在线参数 
	 * 
	 */
	public String getparamOL( String param_name ){
		return MobclickAgent.getConfigParams(this, param_name);
	}

		    
	@Override
	protected void onResume() {
	    super.onResume();
	    MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
	    super.onPause();
	    MobclickAgent.onPause(this);
	}
	
	@Override
	protected void onDestroy() {
		 
	    super.onDestroy();
	    this.unregisterReceiver(br);
	  
	}
	
	
	protected class LockInputFilter implements InputFilter {     
        public CharSequence filter(CharSequence source, int start,     
         int end, Spanned dest, int dstart, int dend) {     
             
         return source.length() < 1 ? dest.subSequence(dstart, dend)     
                                            : "";     
        }     
	}
	protected class UnLockInputFilter implements InputFilter {     
		public CharSequence filter(CharSequence source, int start,     
                int end, Spanned dest, int dstart, int dend) {     
                  return null;  
        }       
	}
	protected void lockInput(EditText et){
		et.setFilters(new LockInputFilter[] { new LockInputFilter()});
		et.setFocusable(false);
		et.setFocusableInTouchMode(false);
	}
	protected void unlockInput(EditText et){
		et.setFilters(new UnLockInputFilter[] { new UnLockInputFilter()});
		et.setFocusable(true);
		et.setFocusableInTouchMode(true);
	}
	
	

	private class MyBroadcastReciver extends BroadcastReceiver {  
		
		@Override
		  public void onReceive(Context context, Intent intent) {
		   String action = intent.getAction();
		   if(action.equals("mylover.finish.action.broadcast")) {
			   if(reciver)
			   ActivityBase.this.finish();
		   }
		}
	}	

	
	class QuitListener{
		boolean mb2main;
		public void setFlag2Main(){
			mb2main=true;
		}
    	public void execute(Activity ctxOut){
    		if(mb2main){
//    			AppMain.changed_trylucky();
    			return;
    		}
    		final Activity ctx=ctxOut;
    		Resources r = CommonDefine.getResources(ctx);
    		final CustomDlg dlg = new CustomDlg(ctx, R.style.mydialog);
    		dlg.setDisplayView(null, r.getString(R.string.toast_confirmquit), r.getString(R.string.toast_confirm), r.getString(R.string.toast_cancel));
    		dlg.setLBtnListner(new View.OnClickListener() {
    			
    			@Override
    			public void onClick(View arg0) {
    				dlg.cancel();
//    				Weibo.releaseInstance();
    	        	   ctx.finish();
    			}
    		});
    		dlg.setRBtnListner(new View.OnClickListener() {
    			
    			@Override
    			public void onClick(View arg0) {
    				dlg.cancel();
    			}
    		});
    		dlg.show();
    	}
    }


	
}